﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

using Game.Command;

namespace SL
{
    public partial class CommandQueueControl 
    {
        ScheduledCommandQueue _CommandQueue;

        public CommandQueueControl()
        {
            InitializeComponent();
        }

        public void Refresh(CommandExecutor commandExecutor)
        {
            stackPanel1.Children.Clear();

            foreach (ScheduledCommand cmd in _CommandQueue)
            {
                var b = new CommandButton(cmd);
                b.HorizontalAlignment = HorizontalAlignment.Stretch;

                b.Content = "" + cmd.Name;
                stackPanel1.Children.Add(b);

                b.Click += (sender, e) => _CommandQueue.Remove(commandExecutor, cmd);
            }
        }

        public void Attach(
            CommandExecutor commandExecutor,
            ScheduledCommandQueue commands)
        {
            _CommandQueue = commands;
            Refresh(commandExecutor);
        }

        class CommandButton : Button
        {
            readonly ScheduledCommand _GameCommand;

            public CommandButton(ScheduledCommand gameCommand)
            {
                _GameCommand = gameCommand;
                Update();

                gameCommand.Scheduled += (source, args) => Update();
            }

            void Update()
            {
                var color = Colors.Black;
                if (_GameCommand.IsRunning)
                {
                    color = Colors.Green;
                }
                Foreground = new SolidColorBrush(color);
            }
        }
    }
}
